package com.lili.order.service.impl.aftersale;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.model.order.entity.PageMap;
import com.lili.model.order.entity.aftersale.AfterSale;
import com.lili.model.order.entity.aftersale.AfterSaleReason;
import com.lili.model.order.entity.sqlException;
import com.lili.order.mapper.aftersale.AfterSaleMapper;
import com.lili.order.mapper.aftersale.AfterSaleReasonMapper;
import com.lili.order.service.aftersale.AfterSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.Map;
@Service
public class AfterSaleServiceImpl implements AfterSaleService {
    @Autowired
    AfterSaleMapper afterSaleMapper;
    @Autowired
    AfterSaleReasonMapper afterSaleReasonMapper;

    @Override
    public int save(AfterSale afterSale) {
        afterSale.setSn(createSn());
        int rs=afterSaleMapper.insert(afterSale);
        if(rs==0){
            return rs;
        }
        AfterSaleReason afterSaleReason=getReason(afterSale);
        rs=afterSaleReasonMapper.insert(afterSaleReason);
        if(rs==0){
            putThrow("订单原因保存失败");
        }
        return rs;

    }

    @Override
    public int delete(Serializable id) {
        return afterSaleMapper.myDeleteById(id);
    }

    @Override
    public int update(AfterSale afterSale) {
        QueryWrapper<AfterSale> wrapper=new QueryWrapper<>();
        wrapper.eq("id",afterSale.getId());
        int rs=  afterSaleMapper.update(afterSale,wrapper);
        if(rs==0){
            return 0;
        }
        if(afterSale.getReason()!=null){
            AfterSaleReason afterSaleReason =new AfterSaleReason();
            afterSaleReason.setReason(afterSale.getReason());
            afterSaleReason.setReasonId(afterSale.getId());
            rs=afterSaleReasonMapper.updateReason(afterSaleReason);
            if(rs==0){
                System.out.println("rs=0有可能售后原因已经被删除");
            }
        }
        return 1;
    }

    @Override
    public Page<AfterSale> queryByPage(Map map) {
        QueryWrapper<AfterSale> wrapper=new QueryWrapper<>();
        wrapper.eq("delete_flag",0);
        if(map.containsKey("createTime")){
            wrapper.ge("create_time", map.get("createTime"));//大于某个时间
        }
        if(map.containsKey("orderSn")){
            wrapper.like("order_sn",map.get("orderSn"));
        }
        if(map.containsKey("sn")){
            wrapper.like("sn",map.get("sn"));
        }
        if(map.containsKey("storeName")){
            wrapper.like("store_name",map.get("storeName"));
        }
        if(map.containsKey("memberName")){
            wrapper.like("member_name",map.get("memberName"));
        }
        if(map.containsKey("storeId")){
            wrapper.like("store_id",map.get("storeId"));
        }
        if(map.containsKey("serviceType")){
            wrapper.like("service_type",map.get("serviceType"));
        }
        PageMap pageMap=new PageMap(map);
        Page<AfterSale> page = new Page<>(pageMap.getPage(), pageMap.getPageSize());
        afterSaleMapper.selectPage(page,wrapper);
        return page;
    }
    @Override
    public AfterSale queryById(Serializable id) {
        return afterSaleMapper.selectById(id);
    }
    public AfterSaleReason getReason(AfterSale afterSale){
        AfterSaleReason afterSaleReason=new AfterSaleReason();
        afterSaleReason.setReasonId(afterSale.getId());
        afterSaleReason.setReason(afterSale.getReason());
        afterSaleReason.setServiceType(afterSale.getServiceType());
        afterSaleReason.setCreateTime(afterSale.getCreateTime());
        return afterSaleReason;
    }
    protected void putThrow(String message) {
        try {
            throw new sqlException(message);
        } catch (sqlException e) {
            throw new RuntimeException(e);
        }
    }
    public String createSn(){
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("当前日期和时间: " + currentDateTime);
        // 获取年、月、日
        int year = currentDateTime.getYear();
        Month currentMonth = Month.JULY;
        String userId="11223344556677";
        int month = currentMonth.getValue();
        int day = currentDateTime.getDayOfMonth();
        int hour = currentDateTime.getHour();
        int minute = currentDateTime.getMinute();
        int second=currentDateTime.getSecond();
        return  "A"+year+formatTime(month)+formatTime(day)+formatTime(hour)+formatTime(minute)+formatTime(second)+userId;
    }
    public String formatTime(int time )
    {
        return String.format("%02d",time);
    }
}
